package com.me.mysqladmin.service;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.me.mysqladmin.dao.TableDataDAO;
import com.me.mysqladmin.dao.TableFieldDAO;
import com.me.mysqladmin.dao.TableStatusDAO;
import com.me.mysqladmin.entity.Location;
import com.me.mysqladmin.entity.TableCreate;
import com.me.mysqladmin.entity.TableData;
import com.me.mysqladmin.entity.TableField;
import com.me.mysqladmin.entity.TableName;
import com.me.mysqladmin.entity.TableStatus;
import com.me.mysqladmin.mapper.TableDataMapper;
import com.me.mysqladmin.mapper.TableStatusMapper;
import com.me.mysqladmin.util.MapUtil;

/**
 * 表格业务层
 * @author dong
 */
@Service("tableService")
public class TableService {
	// 注入持久层组件
	@Resource
	private TableStatusDAO tableStatusDAO;
	@Resource
	private TableDataDAO tableDataDAO;
	@Resource
	private TableFieldDAO tableFieldDAO;
	@Resource
	private TableDataMapper tableDataMapper;
	@Resource
	private TableStatusMapper tableStatusMapper;
	
	/**
	 * 查询指定数据库下的所有表格
	 * @param dbName
	 */
	public List<TableStatus> queryTable(String dbName){
		Assert.hasText(dbName);
		return tableStatusDAO.queryByDb(dbName);
	}
	
	/**
	 * 查询指定数据库下的所有表格（全部列）
	 */
	public List<Map<String, Object>> queryTableMap(String dbName){
		Assert.hasText(dbName);
		return tableStatusDAO.queryMapByDb(dbName);
	}
	
	/**
	 * 查询指定数据库下的所有表格（指定列，与实体属性对应）
	 * @param dbName
	 * @return
	 */
	public List<Map<String, Object>> showTables(String dbName){
		Assert.hasText(dbName);
		List<Map<String, Object>> tables = queryTableMap(dbName);
		for(Map<String, Object> map : tables){
			MapUtil.simplify(map, TableStatus.class);
		}
		return tables;
	}
	
	/**
	 * 查询一张表的所有记录
	 */
	public List<Map<String, Object>> showTableDatas(Location loc){
		Assert.notNull(loc);
		Assert.hasText(loc.getDatabaseName());
		Assert.hasText(loc.getTableName());
		return tableDataDAO.queryDataMap(loc);
	}
	
	/**
	 * 查询一张表的所有字段信息
	 */
	public List<Map<String, Object>> showTableFields(Location loc){
		Assert.notNull(loc);
		Assert.hasText(loc.getDatabaseName());
		Assert.hasText(loc.getTableName());
		List<Map<String, Object>> fields = tableFieldDAO.queryFieldMap(loc);
		for(Map<String, Object> map : fields){
			MapUtil.simplify(map, TableField.class);
		}
		return fields;
	}
	
	/**
	 * 增加一条表记录
	 */
	public Integer insertTableData(TableData tableData) throws Exception {
		Assert.notNull(tableData);
		Assert.notEmpty(tableData.getMap());
		return tableDataMapper.insert(tableData.getMap());
	}
	
	/**
	 * 修改一条标记录
	 */
	public Integer modifyTableData(TableData tableData) throws Exception {
		Assert.notNull(tableData);
		Assert.notEmpty(tableData.getMap());
		return tableDataMapper.update(tableData.getMap());
	}
	
	/**
	 * 获取一张表的主键名
	 */
	public String getPrimaryKey(Location loc){
		Assert.notNull(loc);
		return tableFieldDAO.queryPrimaryKeyCol(loc);
	}
	
	/**
	 * 删除一条记录
	 */
	public Integer deleteTableData(TableData tableData) throws Exception {
		Assert.notNull(tableData);
		Assert.notEmpty(tableData.getMap());
		return tableDataMapper.delete(tableData.getMap());
	}
	
	/**
	 * 删除一张表
	 */
	public Integer deleteTable(TableStatus tableStatus) throws Exception {
		Assert.notNull(tableStatus);
		Assert.notNull(tableStatus.getDatabaseName());
		Assert.notNull(tableStatus.getTable_name());
		return tableStatusDAO.dropTable(tableStatus);
	}
	
	/**
	 * 重命名一张表
	 */
	public void renameTable(TableName tableName) throws Exception{
		Assert.notNull(tableName);
		Assert.notNull(tableName.getDatabaseName());
		Assert.notNull(tableName.getNewName());
		tableStatusDAO.renameTable(tableName);
	}
	
	/**
	 * 创建一张表
	 */
	public void createTable(TableCreate tableCreate) throws Exception {
		Assert.notNull(tableCreate);
		Assert.notEmpty(tableCreate.getFields());
		Assert.hasText(tableCreate.getLocation());
		tableStatusMapper.createTable(tableCreate);
	}
}
